package com.tanhua.dubbo.api.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.domain.db.BlackList;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.TodayBestVo;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static java.util.stream.Collectors.toList;

@Service
public class UserInfoApiImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    BlackListApi blackListApi;
    @Override
    public void saveUserInfo(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    //根据id查找用户的信息
    @Override
    public UserInfo findById(Long id) {
        UserInfo userInfo = userInfoMapper.selectById(id);
        return userInfo;
    }

    @Override
    public Page<UserInfo> findBlackList(Integer pageNum, Integer pagesize, Long userId) {
        //创建一个Page对象
        Page<UserInfo> page = new Page<>(pageNum,pagesize);
        //根据用户id查询当前的用户的黑名单列表, 自定义方法使用mybatis-plus分页要求： 1. 方法的返回值必须是page，2.方法的形参必须要接受Page
        userInfoMapper.findBlackList(page,userId);

        return page;
    }

    @Override
    public Page<UserInfo> findByPage(Integer page, Integer pagesize) {
        //设置当前页与页面大小
        Page<UserInfo> pageInfo = new Page<>(page,pagesize);
        userInfoMapper.selectPage(pageInfo, null);
        return pageInfo;
    }

    @Override
    public List<TodayBestVo> randomRecommended(long userId) {
        Random random = new Random();
        List<TodayBestVo> todayBestVos = new ArrayList<>();
        //查询用户总数
        Long countUserInfo = this.getBaseMapper().countUserInfo();
        //查询用户黑名单
        List<BlackList> blackUsers = blackListApi.findBlack(userId);
        //黑名单id
        List<Long> blackUserIds = new ArrayList<>();
        for (BlackList blackUser : blackUsers) {
            blackUserIds.add(blackUser.getBlackUserId());
        }
        List<Long> randomRecommendedId = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            //随机id
            long id = random.nextInt(countUserInfo.intValue());
            randomRecommendedId.add(id);
        }
        //去交集
        List<Long> result = randomRecommendedId.stream().filter(item -> !blackUserIds.contains(item)).collect(toList());

        if (result.size() > 10) {
            for (int i = 0; i < 10; i++) {
                //伪随机推荐
                // 定义TodayBestVo对象，封装返回的数据
                TodayBestVo todayBestVo = new TodayBestVo();
                //查询用户详细信息
                UserInfo userInfo = this.findById(result.get(i));
                if (userInfo != null) {
                    //  封装用户信息
                    BeanUtils.copyProperties(userInfo, todayBestVo);
                    //  设置tags
                    if (userInfo.getTags() != null) {
                        todayBestVo.setTags(userInfo.getTags().split(","));
                    }
                    todayBestVos.add(todayBestVo);
                }
            }
        } else {
            this.randomRecommended(userId);
        }
        return todayBestVos;


    }
}
